var util = require('../../../libs/util.js');
var app = getApp();
var sliderWidth = 120; // 需要设置slider的宽度，用于计算中间位置
Page({
  data: {
    hasData: true,
    iotBox:[]
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onLoad: function () {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName +"--仓库管理端" });
    wx.showToast({
      title: "数据加载中，请稍候...",
      icon: 'loading',
      duration: 3000
    });
    this.loadData();
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  loadData: function () {
    var that = this;
    wx.request({
      url: app.server+'/wechat/wxapplet/getWmsWarehouse',
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Cookie':app.globalData.cookie
      },
      data: {
        openid: app.globalData.openid
      },
      success: function (res) {
        wx.hideToast();
        console.log(res.data)
        var wh_data = res.data.data[0], whl_data = res.data.data[1], eb_data = res.data.data[2],warehouseData = [];
        var iotbox = that.data.iotbox;
        if (res.data.success && wh_data.length > 0) {
          that.setData({
            hasData: true
          });
          for (var i = 0; i < wh_data.length; i++) {
            warehouseData.push({
              ID: wh_data[i].ID,
              name: wh_data[i].name,
              address: wh_data[i].address,
              iot_box: wh_data[i].iot_box,
              electricbill: '',
              electricbillMonth: '',
              list:[]
            });
            iotbox.push(wh_data[i].iot_box);
          };
          for (var i = 0; i < warehouseData.length; i++) {
            var WID = warehouseData[i].ID, list = warehouseData[i].list;
            for (var j = 0; j < whl_data.length; j++) {
              if (WID == whl_data[j].warehouse){
                list.push({
                  name: whl_data[j].name,
                  admin_name: whl_data[j].admin_name,
                  size: whl_data[j].size,
                  sensor: whl_data[j].sensor,
                  use_percent: whl_data[j].use_percent,
                  NT:'__',
                  HR:'__'
                });
              }
            };
            for (var k = 0; k < eb_data.length; k++) {
              if (WID == eb_data[k].warehouse) {
                warehouseData[i].electricbill = eb_data[k].cost;
                warehouseData[i].electricbillMonth = util.dateformat(eb_data[k].cost_month,"M");
              }
            };
          };
          that.setData({
            warehouseData: warehouseData,
            iotbox: iotbox
          });

          for (var i=0;i<iotbox.length;i++){
            console.log(iotbox[i])
            //app.socket.emit('warehouseMonitor', { iotbox: iotbox[i] });
          }
          
          // else{
          //   socket.emit('vehicleEnd', { iotbox: that.data.iotbox });
          // }

          // app.socket.on('iot', function (data) {
          //   //console.log(data)

          //   var _warehouseData = that.data.warehouseData;
          //   for (var i = 0; i < _warehouseData.length; i++) {
          //     if (_warehouseData[i].iot_box == data.iotbox){
          //       var _list = _warehouseData[i].list;
          //       for (var j = 0; j < _list.length; j++) {
          //         if (_list[j].sensor == data.iot_no){
          //           if (data.iotType == 'HR'){
          //             _list[j].HR = data.data;
          //           } else if (data.iotType == 'NT') {
          //             _list[j].NT = data.data;
          //           }
          //         }
          //       }
          //     }
          //   }
          //   that.setData({
          //     warehouseData: _warehouseData
          //   });
          // });
          

        } else {
          that.setData({
            hasData: false
          })
        };
      },
      fail: function (error) {
        wx.hideToast();
        console.log(error);
        that.setData({
          hasData: false
        })
      }
    })
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    console.log(e.detail.value)
    this.setData({
      inputVal: e.detail.value
    });
  },
  calltodriver: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.target.id
    })
  },
  driverLocation: function (e) {
    var location = String(e.target.id).split("_");
    wx.openLocation({
      latitude: location[0],
      longitude: location[1],
      scale: 28
    })
  },
  warehouseMonitor: function () {
    wx.redirectTo({
      url: '/pages/warehouse/warehouse'
    })
  },
  warehouseStock: function () {
    wx.redirectTo({
      url: '/pages/warehouse/stock'
    })
  },
  goodsManage: function () {
    wx.redirectTo({
      url: '/pages/warehouse/goods'
    })
  }
})
